﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL_Manufacturer
/// </summary>
public class DAL_Manufacturer
{
    DbProvider data = new DbProvider();

    public DataTable Manufacturer_GetByAll()
    {
        DataTable dt = new DataTable();
        string sql = "select * from Manufacturer Order by [Order]";
        try
        {
            dt = data.ExecuteQuery(sql);
        }
        catch (Exception)
        {
            
            throw;
        }
        return dt;
    }
    public DataTable Manufacturer_GetByID(string id)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("select * from Manufacturer where [Id] = {0}", id);
        try
        {
            dt = data.ExecuteQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }

    public DataTable Manufacturer_GetByTop(int top, string where, string order)
    {
        DataTable dt = new DataTable();
        //string sql = string.Format("select top {0} * from Manufacturer where = '{1}' Order by [{2}] DESC,[Order]", top, where,order);
        string sql = "";
        if (top <= 0)
        {
            sql += "SELECT * FROM Manufacturer";
        }
        else
        {
            sql += "SELECT TOP " + top + " * FROM Manufacturer";
        }
        if (where != "")
        {
            sql += " WHERE " + where;
        }
        if (order != "")
        {
            sql += " Order by " + order;
        }
        try
        {
            dt = data.ExecuteQuery(sql);

        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Manufacturer_Insert(DTO_Manufacturer obj)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("INSERT INTO [Manufacturer] VALUES ('{0}','{1}','{2}','{3}','{4}')", obj.Name, obj.Content, obj.Image, obj.Order, obj.Status);

        try
        {
            data.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Manufacturer_UpdateByAll(string Id, DTO_Manufacturer obj)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("UPDATE [Manufacturer] SET [Name] = '{0}',Content = '{1}',[Image] = '{2}',[Order] = '{3}',[Status] = '{4}' WHERE [Id] = '{5}'", obj.Name, obj.Content, obj.Image, obj.Order, obj.Status, Id);

        try
        {
            data.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Manufacturer_UpdateBySet(string Id, string Set)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("UPDATE [Manufacturer] SET {0} WHERE [Id] = '{1}'", Set, Id);

        try
        {
            data.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Manufacturer_Delete(string Id)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("DELETE FROM Manufacturer WHERE Id = '{0}'", Id);

        try
        {
            data.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
}